<?php
class Role_IndexController extends Zend_Controller_Action{		
	/*
	 * Created by: truonghm;
	 * Created date: 29/10/2010
	 * Function: khoi tao cac gia tri thiet lap cua form 
	 */
	function init()
	{
		$this->view->title = Zend_Registry::get('translate')->translate("Header title");
   		$this->view->headTitle($this->view->title, 'PREPEND');		
	}	
	/**
	 * @deprecated: Danh sach nhom quyen
	 * @category: Role
	 * @author: truonghm
	 * Created: 04/04/2011
	 * *@version: 1
	 */
	public function indexAction(){
		$translate=Zend_Registry::get('translate');
		
		//$viewport=Zend_Registry::get('viewport');
		$session = Zend_Registry::get('session');
		
		$layout = new Ofx_Layout($this->getRequest());
		
		$vn_pageSize = $session->config["page_size"];					
				
		$cls_gridConfig= new Ofx_Controller_ExtjsGrid("rolegroup");
		
		$store = $cls_gridConfig->getStore();
		$store->setUrl('/role/list/')
		      ->setBaseParams(array("limit"=>$vn_pageSize))
		      ->attachListener("load",
								new PhpExt_Listener(PhpExt_Javascript::functionDef(null, 
									"myGrid.getSelectionModel().selectRow(0);", array("g")),
									null, 10)
								);
								
		$layout->setOutput($store->getJavascript(false, "store"));
		$layout->setOutput($store->load(new PhpExt_Data_StoreLoadOptions(array("start"=>0,"limit"=>$vn_pageSize))));
	
		
		$grid = $cls_gridConfig->getGrid();
		$grid->setAutoExpandColumn("description")
		     ->setTitle($translate->translate("Role list"));
		
		$layout->setOutput($grid->getJavascript(false, "myGrid"));
		     
		// Center Region		
		$center = new PhpExt_TabPanel();
		//$center->setActiveTab(0);
		
		
		$tabLayout = new PhpExt_Layout_TabLayout();
		$tabLayout->setDeferredRender(true);	

		$center->setActiveTab(0)
		       ->setLayout($tabLayout)
		       ->addItem($grid);
		       
		//get Toolbal       
		$tb = $center->getTopToolbar();
		Ofx_Extjs::getToolbar($tb,$this->getRequest()->getModuleName());
		
		$tb->addSeparator("-");
		$tb->addTextItem(null,$translate->translate("Search"));
		$tb->addSpacer(2);
		$searchField = new PhpExtUx_App_SearchField();
		$searchField->setStore($store)
            		->setWidth(300);
        $tb->addItem(3, $searchField);
        
        $layout->setViewport($center);
		
		//Begin from Edit user===========================================================
		
		$txtId = PhpExt_Form_Hidden::createHidden("id");
		$txtName = PhpExt_Form_TextField::createTextField("name",$translate->translate("Name"));			   
		$txtDescription = PhpExt_Form_HtmlEditor::createHtmlEditor("description",$translate->translate("Description"));
		
		$cls = new Ofx_Controller_ExtjsForm();
		$editFrom = $cls->createFrom("role","editForm");
		$editFrom->setUrl("/role/update/")
				 ->addItem($txtId);

		$layout->setOutput($editFrom->getJavascript(false, "editFrom"));
				 
		$editWin = new PhpExt_Window();
		$editWin->setTitle($translate->translate("Edit role"))
				->setCloseAction('hide')
				->setModal(true)
				->setLayout(new PhpExt_Layout_FitLayout())
				->setWidth(650)				
				->setHeight(400)
				->setBorder(true)
       			->setMinWidth(500)
       			->setMinHeight(400);
	
       	$btnEdit = PhpExt_Button::createTextButton($translate->translate("Edit"))
       				 ->setHandler(PhpExt_Javascript::variable("btnEdit_Click"));
		$btnCancel = PhpExt_Button::createTextButton($translate->translate("Cancel"))
					 ->setHandler(new PhpExt_Handler(PhpExt_Javascript::inlineStm("editWin.hide();")));

		$editWin->addButton($btnEdit);
		$editWin->addButton($btnCancel);
		$editWin->addItem($editFrom);
		
		$layout->setOutput($editWin->getJavascript(false, "editWin"));
		//Begin create form role========================
		
		$cls = new Ofx_Controller_ExtjsForm();
		$insertFrom = $cls->createFrom("role","insertForm");
		$insertFrom->setUrl("/role/insert/");
		
		$layout->setOutput($insertFrom->getJavascript(false, "insertFrom"));
		
		$insertWin = new PhpExt_Window();
		$insertWin->setTitle($translate->translate("Insert role"))
				->setCloseAction('hide')
				->setModal(true)
				->setLayout(new PhpExt_Layout_FitLayout())
       			->setBorder(true)
       			->setMinWidth(500)
       			->setMinHeight(400)
				->setWidth(650)				
				->setHeight(400);		
		
		$btnInsert = PhpExt_Button::createTextButton($translate->translate("Insert"))
					 ->setHandler(PhpExt_Javascript::variable("btnInsert_Click"));
					 					 
		$btnCancel = PhpExt_Button::createTextButton($translate->translate("Cancel"))
					 ->setHandler(new PhpExt_Handler(PhpExt_Javascript::inlineStm("
					  	insertFrom.getForm().reset(); 
					  	insertWin.hide();")));
					 
		$insertWin->addButton($btnInsert);
		$insertWin->addButton($btnCancel);
		$insertWin->addItem($insertFrom);	
		
		$layout->setOutput($insertWin->getJavascript(false, "insertWin"));		
		
		
		// Begin form permit
		
		$treePrivilegeLoader = new PhpExt_Tree_TreeLoader();
		$treePrivilegeLoader->setDataUrl("/role/permit/");
		
		$layout->setOutput($treePrivilegeLoader->getJavascript(false, "treePrivilegeLoader"));
		
		$treePrivilegeRoot = new PhpExt_Tree_AsyncTreeNode();
		$treePrivilegeRoot->setText("Permit");
		
		$layout->setOutput($treePrivilegeRoot->getJavascript(false, "treePrivilegeRoot"));
		
		
		$treePrivilege = new PhpExt_Tree_TreePanel();
		$treePrivilege->setUseArrows(true)
				     ->setAnimate(true)				     
				     ->setEnableDd(true)
				     ->setContainerScroll(true)
				     ->setLoader($treePrivilegeLoader)
				     ->setName('privilege')
				     ->setBorder(true)
				     ->setFieldLabel("Privilege")
				     ->setHeight(200)
				     ->setAutoScroll(true);
		
		$layout->setOutput($treePrivilege->getJavascript(false, "treePrivilege"));
		$layout->setOutput($treePrivilege->setRootNode($treePrivilegeRoot));
		
		$txtRoleName = PhpExt_Form_TextField::createTextField("name",$translate->translate("Name"));
		$txtRoleName->setDisabled(true);
		
		$permitForm  = new PhpExt_Form_FormPanel();
		$permitForm ->setId("permitForm")
					->setWidth('auto')
					->setUrl("/role/privilege/update/")
					->setLabelWidth(100)
					->setBodyStyle("padding: 10px;")
					->setLabelAlign(PhpExt_Ext::HALIGN_RIGHT)
					->setBorder(false)
					->addItem($txtId)
					->addItem($txtRoleName,new PhpExt_Layout_AnchorLayoutData("90%"))
					->addItem($treePrivilege,new PhpExt_Layout_AnchorLayoutData("90%"));
		
		$layout->setOutput($permitForm ->getJavascript(false, "permitForm"));
					
		$permitWin = new PhpExt_Window();
		$permitWin->setTitle($translate->translate("Insert role"))
				->setCloseAction('hide')
				->setIconCssClass("icon-add")
				->setModal(true)
				->setLayout(new PhpExt_Layout_FitLayout())
       			->setBorder(true)
       			->setMinWidth(500)
       			->setMinHeight(400)
				->setWidth(650)				
				->setHeight(400);
				
		$btnCancel = PhpExt_Button::createTextButton($translate->translate("Cancel"))
					 ->setHandler(new PhpExt_Handler(PhpExt_Javascript::inlineStm("
					  	permitForm.getForm().reset(); 
					  	permitWin.hide();")));

		$btnUpdatePrivilage = PhpExt_Button::createTextButton($translate->translate("Edit"))
					 ->setHandler(PhpExt_Javascript::variable("btnEditPrivilege_Click"));
					 
		$permitWin->addButton($btnUpdatePrivilage);
		$permitWin->addButton($btnCancel);
		$permitWin->addItem($permitForm );

		$layout->setOutput($permitWin->getJavascript(false, "permitWin"));		
		
		$this->view->layout= Ofx_Extjs::onReady($layout->getLayout());
		
	}	
}